package com.adams.config.engine;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * 
 * File Name : ConfigEngine.java
 * 
 * @Description : 配置信息缓存类
 * @author 903819 杜峰
 */
public class ConfigEngine
{

	protected Logger logger = LoggerFactory.getLogger(getClass());

	private static ConfigEngine instance;

	// 缓存的内容
	private static Map<String, Config> cache = new HashMap<String, Config>();

	// 错误消息配置文件名称
	// private static final String CONFIG_MESSAGE_FILE_NAME =
	// "config_message.xml";

	private ConfigEngine()
	{
		init();
	}

	public static ConfigEngine getInstance()
	{
		if (instance == null)
		{
			synchronized (ConfigEngine.class)
			{// double check
				if (instance == null)
				{
					instance = new ConfigEngine();
				}
			}
		}
		return instance;
	}

	private void init()
	{

		try
		{
			Document document = null;
			PropertesEngine.load();
			String files[] = PropertesEngine.getProperty("config.file").split(
					",");
			SAXReader reader = new SAXReader();
			for (String file : files)
			{
				document = reader.read(ConfigEngine.class.getClassLoader()
						.getResourceAsStream(file));
				parse(document.getRootElement());
			}

		} catch (DocumentException e)
		{
			logger.error("解析文件出现错误,cause:", e);
			e.printStackTrace();
		} catch (IOException e)
		{
			logger.error("解析文件出现错误,cause:", e);
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private void parse(Element rootElement)
	{
		if (rootElement == null)
		{
			throw new RuntimeException("配置文件错误");
		}
		List<DefaultElement> configElements = rootElement.elements();
		if (configElements != null && configElements.size() > 0)
		{
			for (DefaultElement item : configElements)
			{
				Config config = new Config();
				config.setKey(item.elementText("key"));
				if (config.getKey() == null
						|| config.getKey().trim().length() == 0)
				{
					throw new RuntimeException(
							"配置文件的配置信息错误,原因:存在code为空的配置项,请检查文件");
				}
				config.setValue(item.elementText("value"));
				config.setDescription(item.elementText("description"));
				cache.put(config.getKey(), config);
			}
		}

	}

	/**
	 * 
	 * Description : 读描述信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getDescription(String key)
	{
		Config config = getConfig(key);
		if (config != null)
		{
			return config.getDescription();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public String getValue(String key)
	{
		Config config = getConfig(key);
		if (config != null)
		{
			return config.getValue();
		}
		return null;

	}

	/**
	 * 
	 * Description : 读异常配置信息
	 * 
	 * @param errorCode
	 * @return
	 * 
	 */
	public Config getConfig(String key)
	{
		if (key == null || key.trim().length() == 0)
		{
			return null;
		}
		return cache.get(key);
	}

}
